Glide Record - Scripting

1. To find out number of active incidents

var incident=new GlideRecord('incident');

incident.addQuery('active', 'true');

incident.query();

gs.print("Row count ="+incident.getRowCount());


2. To find out list of active incidents

var incident=new GlideRecord('incident');

incident.addQuery('active', 'true');

incident.query();

gs.print("Row count ="+incident.getRowCount());

gs.print("List of Incidents");

while(incident.next()){

gs.print('Incident number'+incident.number);

}


3. To find out no of incidents assigned to admin userid

var gr= new GlideRecord("incident");

var p= gs.getUserID("admin");

gr.addQuery("assigned_to",p);

gr.query();

gs.print("No of incidents="+gr.getRowCount());

while(gr.next()){

gs.print(gr.number);

}


4. To find out no of incidents assigned to current user

var gr= new GlideRecord("incident");

var p= gs.getUserID();

gr.addQuery("assigned_to",p);

gr.query();

gs.print("No of incidents="+gr.getRowCount());

while(gr.next()){

gs.print(gr.number);

}


5. How many incidents with category hardware?

var g= new GlideRecord("incident");

g.addQuery("category","hardware");

g.query();

gs.print("No of incidents with category hardware="+g.getRowCount());

while(g.next()){

gs.print(g.number);

}


6. How many incidents with “Assigned To” value contains name admin?

var gr=new GlideRecord('incident');

gr.addQuery('assigned_to.name','CONTAINS','admin');

gr.query();

var a= gr.getRowCount();

gs.print(a);

while(gr.next){

gs.print(gr.number);

}


7. Get all the incident which has Assigned to user having first name contains name admin word

var gr= new GlideRecord('incident');

//gr.addEncodedQuery('assigned_toLIKEadmin');

//gr.addEncodedQuery('assigned_to.first_name.admin');

gr.addEncodedQuery('GOTOassigned_to.first_nameLIKEadmin');

/* var user= gs.getUserName('admin');

var admin=gr.addQuery('user',admin);

gr.addQuery('assigned_to',user); */

//gr.addQuery('assigned_to','CONTAINS','admin');

gr.query();

while(gr.next()){

var count=gr.getRowCount();

}

gs.print(count);


8. To find out number incidents which are resolved or closed

var incident=new GlideRecord('incident');

incident.addQuery('state', '6').addOrCondition('state', '7'); // resolved=6 and closed=7

incident.query();

gs.print("Row count ="+incident.getRowCount());


9. Print first 4 Incident's where caller's first name start with Meghana

var gr =new GlideRecord("incident");

gr.addQuery("caller.first_name","STARTSWITH","Meghana");

gr.orderBy('number');

gr.chooseWindow(0, 4);

gr.query();

while(gr.next()){

gs.print(gr.number)

}


10. Print the incident number using sys_id

var gr = new GlideRecord('incident');

if (gr.get('46cd8aaa2fe318104f7fdb9df699b604'))  //Sys id of record

    gs.print(gr.number);


11. Print out first 4 incidents orderby number

var gr = new GlideRecord('incident');

gr.orderBy('number'); // Ascending Order

gr.chooseWindow(0, 4);

gr.query();

while (gr.next()) { 

  gs.print(gr.number);

}


12. Print 10 incidents by descending order of created date

var gr = new GlideRecord('incident');

gr.orderByDesc('sys_created_on');  // this will be descending order but by default will be ascending order and 

gr.setLimit(10);

gr.query();    

while(gr.next()){

gs.print(gr.number);

}



13. Delete the records from Table

var gr = new GlideRecord("incident");


gr.addQuery("state",3); //to delete all inactive incidents


gr.query();


gr.deleteMultiple();


var gr = new GlideRecord("incident");


//to delete one record


if (gr.get("SYS_ID_OF_RECORD"))


gr.deleteRecord();